import { asyncRoutes, constantRoutes } from '@/router'
import { hospitalInfo } from '@/api/user' // getAuthMenu
import Layout from '@/layout'
// import { Message } from 'element-ui'
/**
 * Use meta.role to determine if the current user has permission
 * @param roles
 * @param route
 */
function hasPermission(roles, route) {
    if (route.meta && route.meta.roles) {
        return roles.some((role) => route.meta.roles.includes(role))
    } else {
        return true
    }
}

/**
 * 后台查询的菜单数据拼装成路由格式的数据
 * @param routes
 */
export function generaMenu(routes, data) {
    data.forEach((item) => {
        var metaJsonObj = {}
        metaJsonObj.title = item.name
        metaJsonObj.icon = item.child ? item.icon : ''
        const menu = {
            path: item.url,
            component: item.child ? Layout :
                (resolve) => require([`@/views${item.url}/index`], resolve),
            meta: metaJsonObj,
            children: [],
            name: item.title
        }
        if (item.child) {
            generaMenu(menu.children, item.child)
        }
        routes.push(menu)
    })
}

/**
 * Filter asynchronous routing tables by recursion
 * @param routes asyncRoutes
 * @param roles
 */
export function filterAsyncRoutes(routes, roles) {
    const res = []
    routes.forEach((route) => {
        const tmp = {...route }
        if (hasPermission(roles, tmp)) {
            if (tmp.children) {
                tmp.children = filterAsyncRoutes(tmp.children, roles)
            }
            res.push(tmp)
        }
    })

    return res
}

const state = {
    routes: [],
    addRoutes: [],
    name: '',
    loginTime: '',
    hospitalLogo: '',
    hospitalName: '',
    indexImg: ''
}

const mutations = {
    SET_ROUTES: (state, routes) => {
        state.addRoutes = routes
        state.routes = constantRoutes.concat(routes)
    },
    SET_NAME: (state, name) => {
        state.name = name
    },
    SET_LOGINTIME: (state, loginTime) => {
        state.loginTime = loginTime
    },
    SET_HOSPITALLOGO: (state, hospitalLogo) => {
        state.hospitalLogo = hospitalLogo
    },
    SET_HOSPITALNAME: (state, hospitalName) => {
        state.hospitalName = hospitalName
    },
    SET_INDEXIMG: (state, indexImg) => {
        state.indexImg = indexImg
    }
}

const actions = {
    sethosName({ commit }) {
        return new Promise((resolve) => {
            // 先查询后台并返回左侧菜单数据并把数据添加到路由
            hospitalInfo(state.token)
                .then((response) => {
                    const data = response.data.result
                        // const data = { hos_ico_picture: 'https:', hos_index_pic: 'www.', hos_name: '对账管理系统' }
                    const { hos_ico_picture, hos_index_pic, hos_name } = data
                    // commit('SET_NAME', username)
                    commit('SET_HOSPITALLOGO', hos_ico_picture)
                    commit('SET_HOSPITALNAME', hos_name)
                    commit('SET_INDEXIMG', hos_index_pic)
                })
                .catch((error) => {
                    console.log(error)
                })
        })
    },
    generateRoutes({ commit }, roles) {
        return new Promise((resolve) => {
            const loadMenuData = []
            Object.assign(loadMenuData, roles)
            generaMenu(asyncRoutes, loadMenuData)
            const accessedRoutes = filterAsyncRoutes(asyncRoutes, roles)
            commit('SET_ROUTES', accessedRoutes)
            resolve(accessedRoutes)
                // 先查询后台并返回左侧菜单数据并把数据添加到路由
                // hospitalInfo(state.token)
                //   .then((response) => {
                //     const data = response.data.result
                //     // const data = { hos_ico_picture: 'https:', hos_index_pic: 'www.', hos_name: '对账管理系统' }
                //     const { hos_ico_picture, hos_index_pic, hos_name } = data
                //     const loadMenuData = []
                //     Object.assign(loadMenuData, roles)
                //     generaMenu(asyncRoutes, loadMenuData)
                //     const accessedRoutes = filterAsyncRoutes(asyncRoutes, roles)
                //     commit('SET_ROUTES', accessedRoutes)
                //     // commit('SET_NAME', username)
                //     commit('SET_HOSPITALLOGO', hos_ico_picture)
                //     commit('SET_HOSPITALNAME', hos_name)
                //     commit('SET_INDEXIMG', hos_index_pic)
                //     resolve(accessedRoutes)
                //   })
                //   .catch((error) => {
                //     console.log(error)
                //   })
        })
    }
}
export default {
    namespaced: true,
    state,
    mutations,
    actions
}